/*
* This file is used to define javascript methods which are needed for the "Activities" feature
* to show bar charts for player activities.
*
*/

function handle_day_change(changed) {
	try {
		switch(changed) {
			case 'monday':
			case 'tuesday':
			case 'wednesday':
			case 'thursday':
			case 'friday':
			if (dijit.byId(changed).get("checked") == false) {
				dijit.byId('workingday').set("checked",false);
			} else {
				if (dijit.byId('monday').get("checked") == true &&
				dijit.byId('tuesday').get("checked") == true &&
				dijit.byId('wednesday').get("checked") == true &&
				dijit.byId('thursday').get("checked") == true &&
				dijit.byId('friday').get("checked") == true)

				dijit.byId('workingday').set("checked",true);
			}

			break;
			case 'saturday':
			case 'sunday':
			if (dijit.byId(changed).get("checked") == false) {
				dijit.byId('weekend').set("checked",false);
			} else {
				if (dijit.byId('saturday').get("checked") == true &&
				dijit.byId('sunday').get("checked") == true)

				dijit.byId('weekend').set("checked",true);
			}
			break;
			case 'weekend':
			dijit.byId('saturday').set("checked",false);
			dijit.byId('sunday').set("checked",false);

			if (dijit.byId(changed).get("checked") == true) {
				dijit.byId('saturday').set("checked",true);
				dijit.byId('sunday').set("checked",true);
			}
			break;
			case 'workingday':
			dijit.byId('monday').set("checked",false);
			dijit.byId('tuesday').set("checked",false);
			dijit.byId('wednesday').set("checked",false);
			dijit.byId('thursday').set("checked",false);
			dijit.byId('friday').set("checked",false);

			if (dijit.byId(changed).get("checked") == true) {
				dijit.byId('monday').set("checked",true);
				dijit.byId('tuesday').set("checked",true);
				dijit.byId('wednesday').set("checked",true);
				dijit.byId('thursday').set("checked",true);
				dijit.byId('friday').set("checked",true);
			}

			break;

		}
	} catch(e) {
		alert("handle_day_change: "+e);
	}
}

function reload_url() {
	try {
		var view = dijit.byId("view").get("value");
		if (parseInt(player_id) < 1) return false; // globaly defined in playerinformation.php
		
		var galaxy   = (dijit.byId("act_galaxyview").get("checked") == true) ? 1 : 0;
		var scans    = (dijit.byId("act_scans").get("checked") == true) ? 1 : 0;
		var messages = (dijit.byId("act_messages").get("checked") == true) ? 1 : 0;
		var combats  = (dijit.byId("act_combat_reports").get("checked") == true) ? 1 : 0;
		var allypage = (dijit.byId("act_allypage").get("checked") == true) ? 1 : 0;
		var manual   = (dijit.byId("act_manual").get("checked") == true) ? 1 : 0;
		
		if (view == "day") {
			var monday     = (dijit.byId("monday").get("checked") == true) ? true : false;
			var tuesday    = (dijit.byId("tuesday").get("checked") == true) ? true : false;
			var wednesday  = (dijit.byId("wednesday").get("checked") == true) ? true : false;
			var thursday   = (dijit.byId("thursday").get("checked") == true) ? true : false;
			var friday     = (dijit.byId("friday").get("checked") == true) ? true : false;
			var saturday   = (dijit.byId("saturday").get("checked") == true) ? true : false;
			var sunday     = (dijit.byId("sunday").get("checked") == true) ? true : false;
			var weekend    = (dijit.byId("weekend").get("checked") == true) ? true : false;
			var workingday = (dijit.byId("workingday").get("checked") == true) ? true : false;
		}
	
		var time_from = dojo.date.locale.format(dijit.byId("date_from").get("value"), {datePattern: 'yyyy-MM-dd', selector:'date'});
		var time_to = dojo.date.locale.format(dijit.byId("date_to").get("value"), {datePattern: 'yyyy-MM-dd', selector:'date'});

		var url = "ajax/ajax_activity_graph.php?playerid="+player_id+"&galaxy="+galaxy+"&scans="+scans+"&messages="+messages+"&combats="+combats+"&allypage="+allypage+"&manual="+manual+"&time_from="+time_from+"&time_to="+time_to;
		
		if (view == "day") {
			var days = "";

			if (monday == true)    days+= "1";
			if (tuesday == true)   days+= "2";
			if (wednesday == true) days+= "3";
			if (thursday == true)  days+= "4";
			if (friday == true)    days+= "5";
			if (saturday == true)  days+= "6";
			if (sunday == true)    days+= "7";

			url += "&days="+days;
		} else {
			url += "&weekview=true";
		}
		
		dijit.byId('act_graph').set("href",url);

	} catch(e) {
		alert("unexpected error at reload_url: "+e);
	}
}

function set_player_active(playerid) {
	var source_url = "ajax/ajax_activities.php";

	var xhrArgs = {
	        url: source_url,
	        content : {
	        	"playerid"   : playerid,
				"set_active" : "true"
	    		},
	        handleAs: "json",
	        load: function(jsonData) {
	        	if (jsonData.messages != null) {
	        		showMessages(jsonData.messages);
	        	} else {
	        		reload_url();
	        	}

	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);
}

function set_player_activity(playerid) {
	var source_url = "ajax/ajax_activities.php";
	
	// get date
	var date = dojo.date.locale.format(dijit.byId("date").get("value"), {datePattern: 'yyyy-MM-dd', selector:'date'});
	// get time
	var time_h = dijit.byId("time_h").get("value");
	var time_m = dijit.byId("time_m").get("value");
	
	if (date == "" || time_h == "" || time_m == "") return; // nothing to do
	
	var xhrArgs = {
	        url: source_url,
	        content : {
				"playerid" : playerid,
				"date" : date,
				"time_h" : time_h,
				"time_m" : time_m
	    		},
	        handleAs: "json",
	        load: function(jsonData) {
	        	if (jsonData.messages != null) {
	        		showMessages(jsonData.messages);
	        	} else {
	        		reload_url();
	        	}

	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);
}

function selection_changed(playerid) {
	var view = dijit.byId("view").get("value");
	
	if (view == "week") {
		dojo.byId("day_selection").style.display = "none";
		reload_url();
		
	} else {
		// 24h view
		dojo.byId("day_selection").style.display = "inline";
		reload_url();
		
	}
}
